1 package com.inigoserrano.isvalidator.examples.struts;
2
3 import javax.servlet.http.HttpServletRequest;
4
5 import org.apache.struts.action.ActionError;
6 import org.apache.struts.action.ActionErrors;
7 import org.apache.struts.action.ActionForm;
8 import org.apache.struts.action.ActionMapping;
9
10 import com.inigoserrano.isvalidator.check.DateCheck;
11 import com.inigoserrano.isvalidator.check.EmailCheck;
12 import com.inigoserrano.isvalidator.check.RegularExpresionCheck;
13 import com.inigoserrano.isvalidator.data.SimpleData;
14 import com.inigoserrano.isvalidator.dataGroup.SimpleDataGroup;
15 import com.inigoserrano.isvalidator.errorDo.StrutsErrorDo;
16 import com.inigoserrano.isvalidator.errorDo.StrutsErrorDoGroup;
17
18 /***
19 * This is the ActionForm to capture and validate the parameters
20 *
21 * @author Iņigo Serrano
22 */
23 public class ExampleForm extends ActionForm {
24 /***
25 * The email
26 */
27 private String email = null;
28 /***
29 * The Subject
30 */
31 private String subject = null;
32 /***
33 * The Date
34 */
35 private String date = null;
36 /***
37 * The Body
38 */
39 private String body = null;
40 /***
41 * Constructor for ExampleForm.
42 */
43 public ExampleForm() {
44 super();
45 }
46 /***
47 * @see org.apache.struts.action.ActionForm#validate(ActionMapping, HttpServletRequest)
48 */
49 public ActionErrors validate(
50 ActionMapping mapping,
51 HttpServletRequest request) {
52 try {
53 //The dataGroup
54 SimpleDataGroup inputParameters = new SimpleDataGroup();
55 //For the email
56 SimpleData theEmail = new SimpleData(this.email, "email");
57 theEmail.addCheck(new EmailCheck());
58 theEmail.addCheck(new RegularExpresionCheck(".{1,100}"));
59 //For the subject
60 SimpleData theSubject = new SimpleData(this.subject, "subject");
61 theSubject.addCheck(new RegularExpresionCheck(".{1,255}"));
62 //For the Date
63 SimpleData theDate = new SimpleData(this.date, "date");
64 theDate.addCheck(new DateCheck());
65 //For the Body
66 SimpleData theBody = new SimpleData(this.body, "body");
67 theBody.addCheck(new RegularExpresionCheck(".{1,1000}"));
68 //we must add the Datas to the DataGroupit.
69 inputParameters.addData(theEmail);
70 inputParameters.addData(theSubject);
71 inputParameters.addData(theDate);
72 inputParameters.addData(theBody);
73 //here we are using the dataGroup to check the data
74 if (inputParameters.check()) {
75 //All ok return null
76 return null;
77 } else {
78 //There are errors, so using the errorDo and errorDoGroup
79 //we have the ActionErrors properly configurated
80 inputParameters.setErrorDo(
81 new StrutsErrorDo(),
82 new StrutsErrorDoGroup());
83 //and return it
84 return ((StrutsErrorDoGroup) inputParameters.getErrorDoGroup())
85 .getActionErrors();
86
87 }
88 } catch (Exception e) {
89 ActionError actionError = new ActionError("internalError");
90 ActionErrors actionErrors = new ActionErrors();
91 actionErrors.add(ActionErrors.GLOBAL_ERROR, actionError);
92 return actionErrors;
93 }
94
95 }
96 /***
97 * Returns the body.
98 * @return String
99 */
100 public String getBody() {
101 return body;
102 }
103
104 /***
105 * Returns the date.
106 * @return String
107 */
108 public String getDate() {
109 return date;
110 }
111
112 /***
113 * Returns the email.
114 * @return String
115 */
116 public String getEmail() {
117 return email;
118 }
119
120 /***
121 * Returns the subject.
122 * @return String
123 */
124 public String getSubject() {
125 return subject;
126 }
127
128 /***
129 * Sets the body.
130 * @param body The body to set
131 */
132 public void setBody(String body) {
133 this.body = body;
134 }
135
136 /***
137 * Sets the date.
138 * @param date The date to set
139 */
140 public void setDate(String date) {
141 this.date = date;
142 }
143
144 /***
145 * Sets the email.
146 * @param email The email to set
147 */
148 public void setEmail(String email) {
149 this.email = email;
150 }
151
152 /***
153 * Sets the subject.
154 * @param subject The subject to set
155 */
156 public void setSubject(String subject) {
157 this.subject = subject;
158 }
159
160 }
This page was automatically generated by Maven